{extend name="base"/}
{block name="title"}命令模式3{/block}
{block name="resource"}
<style>

</style>
{/block}
{block name="body"}
<button id="replay">重播</button>
{/block}
{block name="script"}
<script>
    var Ryu = {
        attack: function(){
            console.log( '攻击' );
        },
        defense: function(){
            console.log( '防御' );
        },
        jump: function(){
            console.log( '跳跃' );
        },
        crouch: function(){
            console.log( '蹲下' );
        }
    };

    var commands = {
        "119": "jump",        // W
        "115": "crouch",    // S
        "97": "defense",    // A
        "100": "attack"    // D
    };

    var MakeCommand = function(receiver, state){
        return function(){
            Ryu[ state ]();
        }
    }

    var commandStack = [];

    document.onkeypress = function(ev){
        var keyCode = ev.keyCode;
        if(commands[keyCode]){
            var command = new MakeCommand(Ryu, commands[keyCode]);
            command();
            commandStack.push(command);
        }
    }

    var replayBtn = document.getElementById('replay');
    replayBtn.onclick = function(){
        var command;
        while(command = commandStack.shift()){
            command();
        }
    }
</script>
{/block}

